package com.easylinkin.sm.repository;

import java.util.List;
import javax.transaction.Transactional;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import com.easylinkin.sm.entity.Department;

import site.morn.boot.data.jpa.JpaRepository;

/**
 * 组织机构数据
 *
 * @author wujie
 * @since 0.1.1,  2019/08/19
 */
@Repository
public interface DepartmentRepository extends JpaRepository<Department, Long> {

  @Modifying
  @Transactional
  @Query("update Department set display = false where searchCode like :searchCode")
  void patchHide(@Param("searchCode") String searchCode);

  Long countByCodeAndParentIdAndDisplay(@Param("code") String code,
      @Param("parentId") Long parentId, @Param("display") Boolean display);

  Long countByCodeAndParentIdAndIdNotAndDisplay(@Param("code") String code, @Param("parentId") Long parentId,
      @Param("id") Long id, @Param("display") Boolean display);

  Long countByNameAndParentIdAndDisplay(@Param("name") String name, @Param("parentId") Long parentId,
      @Param("display") Boolean display);

  Long countByNameAndParentIdAndIdNotAndDisplay(@Param("name") String name,
      @Param("parentId") Long parentId, @Param("id") Long id, @Param("display") Boolean display);

  List<Department> findAllBySearchCodeLikeAndDisplay(String searchCode, Boolean display);

  Department findFirstByIdAndDisplay(Long id, Boolean display);
}
